package com.dmwork.service.interaction.controller.front.me;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dmwork.common.base.result.R;
import com.dmwork.common.base.util.JwtInfo;
import com.dmwork.common.base.util.JwtUtils;
import com.dmwork.service.interaction.entity.FeedComment;
import com.dmwork.service.interaction.entity.WorkComment;
import com.dmwork.service.interaction.entity.vo.FeedCommentMessageVo;
import com.dmwork.service.interaction.entity.vo.WorkCommentMessageVo;
import com.dmwork.service.interaction.feign.CustomerFeignService;
import com.dmwork.service.interaction.feign.MessageFeignService;
import com.dmwork.service.interaction.mq.sender.MessageSender;
import com.dmwork.service.interaction.service.FeedCommentService;
import com.google.gson.Gson;
import com.vdurmont.emoji.EmojiParser;
import io.swagger.annotations.ApiOperation;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 动态评论表 前端控制器
 * </p>
 *
 * @author 李天
 * @since 2021-05-07
 */
@Transactional
@RestController
@RequestMapping("/front/dm_work/interaction/feed_comment/me")
public class FrontMeFeedCommentController {

    @Autowired
    private FeedCommentService feedCommentService;
    @Autowired
    private MessageSender messageSender;

    @ApiOperation("直接对动态发表评论")
    @PostMapping("publish_comment")
    public R publishComment(HttpServletRequest request, @RequestBody FeedComment feedComment) {
        JwtInfo jwtInfo = JwtUtils.getCustomerInfoByJwtToken(request);
        feedComment.setCustomerId(jwtInfo.getId());
        String repliedCustomerId = feedComment.getRepliedCustomerId();
        //转换Emoji
        if (!StringUtils.isEmpty(feedComment.getContent())) {
            feedComment.setContent(EmojiParser.parseToAliases(feedComment.getContent()));
        }
        feedCommentService.save(feedComment);
        //如果评论的不是自己的动态，才让消息系统为被评论动态所属的用户创建消息
        if (!repliedCustomerId.equals(jwtInfo.getId())) {
            messageSender.sendCommentMessage(repliedCustomerId, feedComment.getId(), 1, 1);
        }
        return R.ok().message("发布成功");
    }

    @ApiOperation("当前用户回复他人的评论")
    @PostMapping("publish_reply")
    public R publishReply(HttpServletRequest request, @RequestBody FeedComment feedComment) {
        JwtInfo jwtInfo = JwtUtils.getCustomerInfoByJwtToken(request);
        feedComment.setCustomerId(jwtInfo.getId());
        String repliedCustomerId = feedComment.getRepliedCustomerId();
        //转换Emoji
        if (!StringUtils.isEmpty(feedComment.getContent())) {
            feedComment.setContent(EmojiParser.parseToAliases(feedComment.getContent()));
        }        feedCommentService.save(feedComment);
        //如果评论的不是自己的动态，才向MQ发送新评论信息，由service-message-center接收并创建消息
        if (!repliedCustomerId.equals(jwtInfo.getId())) {
            messageSender.sendCommentMessage(feedComment.getRepliedCustomerId(), feedComment.getId(), 1, 1);
        }
        return R.ok().message("发布成功");
    }

    @ApiOperation("删除自己的评论")
    @DeleteMapping("remove_comment/{commentId}")
    public R removeComment(HttpServletRequest request, @PathVariable String commentId) {
        JwtInfo jwtInfo = JwtUtils.getCustomerInfoByJwtToken(request);
        FeedComment feedComment = feedCommentService.getOne(new QueryWrapper<FeedComment>().eq("customer_id", jwtInfo.getId()).eq("id", commentId));
        if (feedComment == null) {
            return R.error().message("这不是您的评论，无法删除");
        }
        feedCommentService.removeById(commentId);
        messageSender.sendCommentMessage(feedComment.getRepliedCustomerId(), commentId, 0, 0);
        return R.ok().message("删除成功");
    }

}

